package com.qianniu.napi.common.util;

import com.qianniu.napi.admin.entity.MediaDO;
import com.qianniu.napi.admin.entity.wxbean.*;

import java.util.Date;
import java.util.List;

/**
 * Created by chenxuebin on 16/8/10.
 */
public class MessageResponse {

    /**
     * 回复文本消息
     *
     * @param fromUserName
     * @param toUserName
     * @param respContent
     * @return
     */
    public static String getTextMessage(String fromUserName, String toUserName, String respContent) {

        TextMessage textMessage = new TextMessage();
        textMessage.setToUserName(fromUserName);
        textMessage.setFromUserName(toUserName);
        textMessage.setCreateTime(new Date().getTime());
        textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
        textMessage.setFuncFlag(0);

        if (respContent.length() > 700) respContent = respContent.substring(0, 700) + "更多详情请联系客服...";

        textMessage.setContent(respContent);
        return MessageUtil.textMessageToXml(textMessage);
    }

    /**
     * 转发客服消息
     *
     * @param fromUserName
     * @param toUserName
     * @return
     */
    public static String zfkefuMessage(String fromUserName, String toUserName,String createTime) {
//        <xml>
//            <ToUserName><![CDATA[touser]]></ToUserName>
//            <FromUserName><![CDATA[fromuser]]></FromUserName>
//            <CreateTime>1399197672</CreateTime>
//            <MsgType><![CDATA[transfer_customer_service]]></MsgType>
//        </xml>
// <xml>
//    <ToUserName><![CDATA[okE_w0PqtiKDsv0vEHp5I3y_oGlg]]></ToUserName>
//    <FromUserName><![CDATA[gh_0125de83d043]]></FromUserName>
//    <CreateTime><![CDATA[1600695149915]]></CreateTime>
//    <MsgType><![CDATA[transfer_customer_service]]></MsgType>
//    <FuncFlag><![CDATA[0]]></FuncFlag>
//</xml>

        TextMessage textMessage = new TextMessage();
        //fromUserName 和 toUserName 互换
        textMessage.setToUserName(fromUserName);
        textMessage.setFromUserName(toUserName);
        textMessage.setCreateTime(Long.valueOf(createTime));
        textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_ZF);

        return MessageUtil.textMessageToXml(textMessage);
    }

    /**
     * 创建图文消息
     *
     * @param fromUserName
     * @param toUserName
     * @param articleList
     * @return
     */
    public static String getNewsMessage(String fromUserName, String toUserName, List<Article> articleList) {

        NewsMessage newsMessage = new NewsMessage();
        newsMessage.setToUserName(fromUserName);
        newsMessage.setFromUserName(toUserName);
        newsMessage.setCreateTime(new Date().getTime());
        newsMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);
        newsMessage.setFuncFlag(0);

        // 设置图文消息个数
        newsMessage.setArticleCount(articleList.size());
        // 设置图文消息包含的图文集合
        newsMessage.setArticles(articleList);
        // 将图文消息对象转换成xml字符串
        return MessageUtil.newsMessageToXml(newsMessage);
    }

    /**
     * 创建图文消息
     *
     * @param fromUserName
     * @param toUserName
     * @param mediaId
     * @return
     */
    public static String getImageMessage(String fromUserName, String toUserName, String mediaId) {

        ImageMessage message = new ImageMessage();
        message.setToUserName(fromUserName);
        message.setFromUserName(toUserName);
        message.setCreateTime(new Date().getTime());
        message.setMsgType(MessageUtil.REQ_MESSAGE_TYPE_IMAGE);
        message.setFuncFlag(0);

        Image image = new Image();
        image.setMediaId(mediaId);

        message.setImage(image);

        // 将图文消息对象转换成xml字符串
        return MessageUtil.imageMessageToXml(message);
    }

    /**
     * 创建视频消息
     *
     * @param fromUserName
     * @param toUserName
     * @param media
     * @return
     */
    public static String getVedioMessage(String fromUserName, String toUserName, MediaDO media) {

        VedioMessage message = new VedioMessage();
        message.setToUserName(fromUserName);
        message.setFromUserName(toUserName);
        message.setCreateTime(new Date().getTime());
        message.setMsgType(MessageUtil.REQ_MESSAGE_TYPE_VEDIO);

        Video video = new Video();
        video.setMediaId(media.getMid());
        video.setTitle(media.getTitle());
        video.setDescription(media.getNote());

        message.setVideo(video);

        // 将图文消息对象转换成xml字符串
        return MessageUtil.videoMessageToXml(message);
    }

}
